package com.huicoo.forestmanager.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class StringUtil {
    public static boolean isEmpty(String data) {
        return data == null || data.length() == 0;
    }

    public static String timeToString(long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss", Locale.CHINA);
        return simpleDateFormat.format(time);
    }

    public static String getString(Context context, int id) {
        return context.getResources().getString(id);
    }

    public static String getUrlParam(String url, String key) {
        try {
            Uri uri = Uri.parse(url);
            return uri.getQueryParameter(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String formatArea(double area) {
        if (area >= 1000) {
            return new DecimalFormat("0.00").format(area / 1000000) + "km²";
        }
        return new DecimalFormat("0.00").format(area) + "m²";
    }


    public static String formatPerimeter(double perimeter) {
        if (perimeter >= 1000) {
            return new DecimalFormat("0.00").format(perimeter / 1000) + "km";
        }
        return new DecimalFormat("0.00").format(perimeter) + "m";
    }

    public static String getGeoPointKey(String id) {
        if (TextUtils.isEmpty(id)) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd" + "_" + id, Locale.getDefault());
        return format.format(new Date());
    }
}
